Grokking Google Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Google Easy
Add Binary (easy)
Solution: Add Binary
Valid Mountain Array (easy)
Solution: Valid Mountain Array
Set Mismatch (easy)
Solution: Set Mismatch
Is Subsequence (easy)
Solution: Is Subsequence
Length of Last Word (easy)
Solution: Length of Last Word
Search Insert Position (easy)
Solution: Search Insert Position
Repeated Substring Pattern (easy)
Solution: Repeated Substring Pattern
Monotonic Array (easy)
Solution: Monotonic Array
Count Nodes Equal to Average of Subtree (easy)
Solution: Count Nodes Equal to Average of Subtree
Find the Difference (easy)
Solution: Find the Difference
How Many Numbers Are Smaller Than the Current Number (easy)
Solution: How Many Numbers Are Smaller Than the Current Number
Binary Tree Paths (easy)
Solution: Binary Tree Paths
Find the Difference of Two Arrays (easy)
Solution: Find the Difference of Two Arrays
Merge Strings Alternately (easy)
Solution: Merge Strings Alternately
Reverse a LinkedList (easy)
Solution: Reverse a LinkedList
Gray Code (medium)
Solution: Gray Code
2 Keys Keyboard (medium)
Solution: 2 Keys Keyboard
Arithmetic Slices (medium)
Solution: Arithmetic Slices
Unique Length-3 Palindromic Subsequences (medium)
Solution: Unique Length-3 Palindromic Subsequences
Minimum Difference Between Largest and Smallest Value in Three Moves (medium)
Solution: Minimum Difference Between Largest and Smallest Value in Three Moves
Letter Combinations of a Phone Number (medium)
Solution: Letter Combinations of a Phone Number
01 Matrix (medium)
Solution: 01 Matrix
Maximum Points You Can Obtain from Cards (medium)
Solution: Maximum Points You Can Obtain from Cards
Strobogrammatic Number II (medium)
Solution: Strobogrammatic Number II
Maximum Length of Semi-Decreasing Subarrays (medium)
Solution: Maximum Length of Semi-Decreasing Subarrays
Minimum Area Rectangle (medium)
Solution: Minimum Area Rectangle
Count Unreachable Pairs of Nodes in an Undirected Graph (medium)
Solution: Count Unreachable Pairs of Nodes in an Undirected Graph
Single Number II (medium)
Solution: Single Number II
4Sum II (medium)
Solution: 4Sum II
Furthest Building You Can Reach (medium)
Solution: Furthest Building You Can Reach
Minimizing Array After Replacing Pairs With Their Product (medium)
Solution: Minimizing Array After Replacing Pairs With Their Product
Fair Distribution of Cookies (medium)
Solution: Fair Distribution of Cookies
Find Leaves of Binary Tree (medium)
Solution: Find Leaves of Binary Tree
Rotate Image (medium)
Solution: Rotate Image
Combination Sum (medium)
Solution: Combination Sum
Implement Trie (Prefix Tree) (medium)
Solution: Implement Trie (Prefix Tree)
Number of Islands (medium)
Solution: Number of Islands
Edit Distance (medium)
Solution: Edit Distance
Container With Most Water (Medium)
Solution: Container With Most Water
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Shortest Subarray with Sum at Least K (hard)
Solution: Shortest Subarray with Sum at Least K
Reverse Pairs (hard)
Solution: Reverse Pairs
Nth Magical Number (hard)
Solution: Nth Magical Number
Candy (hard)
Solution: Candy
Arithmetic Slices II - Subsequence (hard)
Solution: Arithmetic Slices II - Subsequence
Burst Balloons (hard)
Solution: Burst Balloons
Sum of Distances in Tree (hard)
Solution: Sum of Distances in Tree
Maximum Score of a Good Subarray (hard)
Solution: Maximum Score of a Good Subarray
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Solution: Find the Difference

Problem Statement

You are given two strings s and t. The string t is formed by taking the string s, randomly shuffling all its characters, and then adding one additional character at a random place.

Return the extra character that was added to t.

Examples

  • Example 1:

    • Input: s = "a", t = "aa"
    • Expected Output: "a"
    • Justification: The string t contains an extra "a" compared to the string s.
  • Example 2:

    • Input: s = "xyz", t = "yxzz"
    • Expected Output: "z"
    • Justification: Besides the characters in s, t has an additional "z".
  • Example 3:

    • Input: s = "lampump", t = "lumpmpap"
    • Expected Output: "p"
    • Justification: After shuffling s and adding an extra character, t includes one more "p" than s.

Solution

To solve this problem, we will use a character frequency count approach. This method involves counting the frequency of each character in both strings s and t, and then comparing these frequencies to find the character that is extra in t.

This approach is effective because, by identifying the character with a frequency discrepancy, we can pinpoint the additional character in t. This method ensures accuracy regardless of the shuffle, as the order of characters does not impact the frequency count. The simplicity and directness of comparing character frequencies make this approach not only intuitive but also efficient for identifying the added character.

Step-by-Step Algorithm

  1. Initialize a Frequency Counter: Start by creating a data structure (like a hash map or dictionary) to keep track of the frequency of each character in the strings s and t. This will allow us to count how many times each character appears in both strings.

  2. Count Frequencies in s: Iterate through each character in the string s. For each character, increase its count in the frequency counter by 1. This step helps us understand the composition of s in terms of its characters.

  3. Identify the Extra Character in t: Now, iterate through each character in the string t. Decrease its count in the frequency counter by 1 for each occurrence. If at any point, the count for a character goes negative, it indicates that this character is the additional one in t (since it appears more times in t than in s).

  4. Return the Extra Character: The moment you identify a character whose count goes negative, return it. This character is the one that was added to s to form t.

Algorithm Walkthrough

Let's walk through the algorithm using the given strings s = "lampump" and t = "lumpmpap".

  1. Initialize Frequency Map: Start with an empty map.

    {}

  2. Count Characters in s:

    • For 'l': {'l': 1}
    • For 'a': {'l': 1, 'a': 1}
    • For 'm': {'l': 1, 'a': 1, 'm': 1}
    • For 'p': {'l': 1, 'a': 1, 'm': 1, 'p': 1}
    • For 'u': {'l': 1, 'a': 1, 'm': 1, 'p': 1, 'u': 1}
    • For 'm': {'l': 1, 'a': 1, 'm': 2, 'p': 1, 'u': 1} (incrementing 'm')
    • For 'p': {'l': 1, 'a': 1, 'm': 2, 'p': 2, 'u': 1} (incrementing 'p')
  3. Adjust Counts for Characters in t:

    • For 'l': {'l': 0, 'a': 1, 'm': 2, 'p': 2, 'u': 1}
    • For 'u': {'l': 0, 'a': 1, 'm': 2, 'p': 2, 'u': 0}
    • For 'm': {'l': 0, 'a': 1, 'm': 1, 'p': 2, 'u': 0}
    • For 'p': {'l': 0, 'a': 1, 'm': 1, 'p': 1, 'u': 0}
    • For 'm': {'l': 0, 'a': 1, 'm': 0, 'p': 1, 'u': 0}
    • For 'p': {'l': 0, 'a': 1, 'm': 0, 'p': 0, 'u': 0}
    • For 'a': {'l': 0, 'a': 0, 'm': 0, 'p': 0, 'u': 0}
    • For 'p': {'l': 0, 'a': 0, 'm': 0, 'p': -1, 'u': 0} (Now, 'p' count is -1, which indicates 'p' is the added character)
  4. Identify the Added Character: Since the frequency of 'p' went to -1, we conclude that 'p' is the character added to t. Hence, 'p' is returned as the added character.

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity

The time complexity for all implementations is , where (n) is the length of the longer string (t). This is because each character in both strings s and t is visited exactly once to increment or decrement their count in the frequency map or object.

Space Complexity

The space complexity is in terms of the extra space used by the frequency map. Although the size of the map depends on the number of distinct characters, since the input is limited to lowercase or uppercase letters of the English alphabet, the maximum size of the map is constrained to a constant value (26 for either lowercase or uppercase exclusively, 52 if both cases are considered).

Find the Difference (easy)
How Many Numbers Are Smaller Than the Current Number (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity